import matplotlib.pyplot as plt
from matplotlib.ticker import NullLocator
from typing import TYPE_CHECKING

import visualizer.common as v

if TYPE_CHECKING:
    from puzzle.board import Board
    from matplotlib.axes import Axes


def visualize_board(board: "Board") -> "Axes":
    ax: "Axes"
    
    fig, ax = plt.subplots()
    fig.canvas.manager.set_window_title(board.name) # type: ignore
    
    v.prepare_ax(ax)
    v.plot_board(ax, board)
    
    ax.xaxis.set_major_locator(NullLocator())
    ax.yaxis.set_major_locator(NullLocator())
    plt.subplots_adjust(top = 1, bottom = 0, right = 1, left = 0, hspace = 0, wspace = 0) # https://blog.csdn.net/jifaley/article/details/79687000
    plt.margins(0, 0)
    
    fig.savefig(board.name, dpi = 300, transparent = True, pad_inches = 0)
    
    return ax
    
def visualize_board_all(boards: "list[Board]") -> list["Axes"]:
    ret = []
    for i in range(len(boards)):
        fig = plt.figure(i)
        fig.canvas.manager.set_window_title(boards[i].name) # type: ignore
        ax = fig.add_subplot(1, 1, 1)
        
        v.prepare_ax(ax)
        v.plot_board(ax, boards[i])
        
        # fig.savefig(descl[i].name, dpi=200, transparent=True)
    
        ret.append(ax)
    return ret